/**
 * Created with Vim7.3 Mac
 * @fileOverview : 
 * @author : Lyle lylechen2014@gmail.com
 * @since : 2016-08-31 15:06:42
 * @filename : src/base/config.js
 * @version : 
 * @description : 
 */

function queryString(item){
    var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
    return svalue ? svalue[1] : svalue;
};

function getApiAccessUrl(url){
    var mock = queryString('mock'),
        sid = queryString('sid');
    if (mock) {
        apiAccessUrl =  url+'s'+queryString('sid')+'/';
    } else {
        if (location.href.indexOf("?dev") > 0) {
            apiAccessUrl = url+'s'+getCookie('session_id')+'/';
        } else {
            apiAccessUrl = url;
        }
    }
    return apiAccessUrl;
};

function getLocale(){
    var locale = getCookie('siteLocale');
    return locale == '' ? 'en' : local;
};

function getCookie(name) {
    var search = name + "=",offset,end; 
    if(document.cookie.length > 0) { 
        offset = document.cookie.indexOf(search);
        if(offset != -1) { 
            offset += search.length;
            end = document.cookie.indexOf(";", offset); 
            if(end == -1) {
                end = document.cookie.length;
            };
            return decodeURI(document.cookie.substring(offset, end));
        } 
    }
    return "";
};

seajs.config({
    alias: {
        "baseLang" : remoteUrl+"/base/i18n/"+getLocale()+".js",
        "lang" : remoteUrl+"/"+engine+"/i18n/"+getLocale()+".js",
        "$": "tools/jQuery/jquery-1.12.4.js",
        "underscore": "tools/underscore/underscore-1.8.3.js",
        "director": "tools/director/director-1.2.6.js",
        "util": "tools/utils/util.js",
        "baseShare": remoteUrl+"/base/share",
        "baseConfig": remoteUrl+"/base/config",
        "config": remoteUrl+"/"+engine+"/config",
    },

    preload: [
        '$', 'director','underscore'
    ],

    paths: {
        'tools': remoteUrl+'/base/tools'
    },

    vars: {
        'base': remoteUrl+'/base',
        'site' : remoteUrl+'/'+engine,
        'locale': getLocale(),               
        'apiUrl': apiUrl,
        'apiAccessUrl': getApiAccessUrl(apiUrl),
    },

    debug: true,

    base: remoteUrl+'/component/',

    charset: 'utf-8',

    'map': [
        [/^(.*\.(?:css|js|tpl))(.*)$/i, '$1?v='+version]
    ],
});

seajs.use(['$','underscore','director','lang','baseLang','baseShare','baseConfig','config','util'],function($, underscore,director,lang,baseLang,baseShare,baseConfig,config,util){
    window.Router = director;
    window._ = underscore;
    window.$ = $;
    window.Util = util;
    window.Share = baseShare;
    window.Lang = _.extend(baseLang,lang);
    window.Config = _.extend(baseConfig,config);
    seajs.use(remoteUrl+"/base/main");
});

